﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace LINQtoFxCop.Core
{
  public static class IEnumerableExtender
  {
    public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> func)
    {
      foreach (var item in source)
        func(item);
      return source;
    }
    public static IEnumerable<QueryResult> Top(this IEnumerable<QueryResult> source, int top)
    {
      return source.OrderByDescending(result => result.Value).Take(top);
    }
    public static bool HasItem<T>(this IEnumerable<T> collection)
    {
      return collection.GetEnumerator().MoveNext();
    }
    public static bool HasItemWhen<T>(this IEnumerable<T> collection, Predicate<T> predicate)
    {
      return collection.Where(obj => predicate.Invoke(obj)).HasItem();
    }
  }
}
